import { sqlMain } from '@utils/dbconf'
import { htmap } from '@maps/htmap'
import { usermap } from '@maps/index'
import { success,error,SQL_ERROR } from '@utils/response'
import { analysis } from '@utils/request'
import redisDB from '@utils/redisconf'
import fs from 'fs'
import { toToken,tokenTo,changeToken } from '@utils/tokenOper'
import htindex from '@/staticdata/htindex.js'
import { dateFormat } from '@utils/functions'
import axios from 'axios'const { Random,mock } = require('mockjs')
const CPTT_IMG_PATH = 'file/cpttimg/'

export const getmycptt = async (req,res) => {
try{
  const { id } = res.locals
  const cptts = await sqlMain(htmap.getmycptt,[id])
  const newdate = new Date()
  for(const item of cptts){
    let { imgname,url,overtime,goingtime,uid } = item
    if(imgname)
      item.imgname = `${CPTT_IMG_PATH}${imgname}`
    if(!url) delete item.url
    overtime = new Date(overtime)
    goingtime = new Date(goingtime)
    item.overtime = dateFormat('YYYY-MM-DD hh:mm:ss',overtime)
    item.goingtime = dateFormat('YYYY-MM-DD hh:mm:ss',goingtime)
    item.state = (newdate < overtime)? 2:(newdate < goingtime)? 3:5
  }
  return res.send(success({cptts}))
}catch(e){
  console.log(e)
  return res.send(SQL_ERROR)}
}

export const gethiscptt = async (req,res) => {
try{
  const { id } = res.locals
  const cptts = await sqlMain(htmap.gethiscptt,[id])
  const newdate = new Date()
  for(const item of cptts){
    const { enddate,championid } = item
    item.enddate = dateFormat('YYYY-MM-DD hh:mm:ss',enddate)
    delete item.uid
    delete item.isUse
    item.champion = []
    for(const id of championid.split(",")){
      let champ = await redisDB.get(3,id)
      if(!champ){
        champ = await sqlMain(usermap.selectname,[id],true).then(e=>e.name)
        await redisDB.set(3,id,champ,10)
      }
      item.champion.push(champ)
    }
    item.url = 'https://api.r10086.com/img-api.php?type=少女写真'+Math.ceil(Math.random()*6)
    delete item.championid
  }
  return res.send(success({cptts}))
}catch(e){
  console.log(e)
  return res.send(SQL_ERROR)}
}

export const getranking = async (req,res) => {
  return res.send(SQL_ERROR)
}

export const addcptt = async (req,res) => {
try{
  const { id,data } = res.locals
  const { name,url,type,overtime,goingtime,info,requirements,isShow } = data
  await sqlMain(htmap.addcptt,[id,name,type,url,overtime,goingtime,info,requirements,isShow ])
  return res.send(success({}))
}catch(e){
  console.log(e)
  return res.send(SQL_ERROR)}
}

export const getdelcptt = async (req,res) => {
try{
  const { id } = res.locals
  const cptts = []
  const cpt = await sqlMain(htmap.selectdiscptt,[id])
  for(const item of cpt){
    const { id,name,type,his=false,url,} = item
    let { imgname } = item
    if(imgname) imgname = `${CPTT_IMG_PATH}${imgname}`
    cptts.push({id,name,type,his,imgname,url})
  }
  const hiscpt = await sqlMain(htmap.selectdishiscptt,[id])
  for(const item of hiscpt){
    const { id,name,type,his=true,url} = item
    cptts.push({id,name,type,his,url})
  }
  return res.send(success({cptts}))
}catch(e){
  return res.send(SQL_ERROR)}
}

export const delcptt = async (req,res) => {
try{
  const { id } = res.locals.data
  await sqlMain(htmap.delcptt,[id])
  return res.send(success({}))
}catch(e){
  return res.send(SQL_ERROR)}
}

export const recovercptt = async (req,res) => {
try{
  const { id } = res.locals.data
  await sqlMain(htmap.recovercptt,[id])
  return res.send(success({}))
}catch(e){
  return res.send(SQL_ERROR)}
}

export const deepdelcptt = async (req,res) => {
try{
  return res.send(error(500,'深度删除!!!!!!!'))
}catch(e){
  return res.send(SQL_ERROR)}
}